package com.xncoding.easypoi.util;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.alibaba.fastjson.JSON;
import org.apache.poi.ss.formula.functions.T;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *   操作注解
 * </p>
 *
 * @author Shen_hs
 * @since 2021-03-04
 */
public class ExcelConvert {

    public static Map<String,String> convertInput(Object obj, final Map<String,String> fieldMap) throws NoSuchFieldException, IllegalAccessException {
        Class<T> clazz = (Class<T>) obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        Map<String ,String > resMap = new HashMap<>();
        for (Field field : fields) {
            Excel annotation = field.getAnnotation(Excel.class);

            if (annotation == null){
                continue;
            }
            String name = fieldMap.get(annotation.fixedIndex()+"");
            if (name == null){
                continue;
            }
            String filedname = annotation.name();
            resMap.put(name,getFieldValueByFieldName(filedname,obj));
        }
        System.out.println("---------------------");
        System.out.println(JSON.toJSONString(obj));
        System.out.println(JSON.toJSONString(resMap));
        return  resMap;
    }
    public static Map<String,String> convertInputObj(Object obj, final Map<String,String> fieldMap) throws NoSuchFieldException, IllegalAccessException {
        Class<T> clazz = (Class<T>) obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        Map<String ,String > resMap = new HashMap<>();
        for (Field field : fields) {
            Excel annotation = field.getAnnotation(Excel.class);

            if (annotation == null){
                continue;
            }
            String name = fieldMap.get(annotation.fixedIndex()+"");
            if (name == null){
                continue;
            }
            String filedname = annotation.name();
            resMap.put(name,getFieldValueByFieldName(filedname,obj));
        }
        System.out.println("---------------------");
        System.out.println(JSON.toJSONString(obj));
        System.out.println(JSON.toJSONString(resMap));
        return  resMap;
    }

    /**
     * 根据属性名获取属性值
     *
     * @param fieldName
     * @param object
     * @return
     */

    protected static String getFieldValueByFieldName(String fieldName, Object object) {
        try {
            Field field = object.getClass().getDeclaredField(fieldName);
            //设置对象的访问权限，保证对private的属性的访问
            field.setAccessible(true);
            return (String) field.get(object);
        } catch (Exception e) {
            return null;
        }
    }


    public static <T> void convert(T obj,final Map<String,String> fieldMap) throws NoSuchFieldException, IllegalAccessException {
        Class<T> clazz = (Class<T>) obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            Excel annotation = field.getAnnotation(Excel.class);

            if (annotation == null){
                continue;
            }
            System.out.println(annotation.fixedIndex());
            String name = fieldMap.get(annotation.fixedIndex()+"");
            if (name == null){
                continue;
            }
            setAnnoName(annotation,name);
        }
        setAnnoCoulmnHidden(obj); //null值列不导出
    }

    private static <T> void setAnnoCoulmnHidden(T obj) throws NoSuchFieldException, IllegalAccessException{
        Class<T> clazz = (Class<T>) obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Object o = field.get(obj);
            if (null == o) {
                Excel annotation = field.getAnnotation(Excel.class);
                if (annotation == null) {
                    continue;
                }
                //获取 foo 这个代理实例所持有的 InvocationHandler
                InvocationHandler h = Proxy.getInvocationHandler(annotation);
                // 获取 AnnotationInvocationHandler 的 memberValues 字段
                Field hField = h.getClass().getDeclaredField("memberValues");
                // 因为这个字段事 private final 修饰，所以要打开权限
                hField.setAccessible(true);
                // 获取 memberValues
                Map memberValues = (Map) hField.get(h);
                // 修改 value 属性值
                memberValues.put("isColumnHidden", true);
            }
        }

    }

    public static void setAnnoName(Excel e,String name) throws NoSuchFieldException, IllegalAccessException {
        //获取 foo 这个代理实例所持有的 InvocationHandler
        InvocationHandler h = Proxy.getInvocationHandler(e);
        // 获取 AnnotationInvocationHandler 的 memberValues 字段
        Field hField = h.getClass().getDeclaredField("memberValues");
        // 因为这个字段事 private final 修饰，所以要打开权限
        hField.setAccessible(true);
        // 获取 memberValues
        Map memberValues = (Map) hField.get(h);
        // 修改 value 属性值
        memberValues.put("name", name);

    }

}


